package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

/**
 * @author hqthai01 Jan 2, 2015
 */
public class JMessageDialog extends JDialog {
	private static final long serialVersionUID = 0xAB1L;

	public JMessageDialog(JFrame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		this.setIconImage(parent.getIconImage());
		this.setTitle("Message");
	}

	private void initComponents() {

		picturePanel = new JPanel(){
			private static final long serialVersionUID = 0x3A1L;

			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Dimension d = getSize();
				Image image = new ImageIcon("src/icons/error.png").getImage();
				g.drawImage(image, 0, 0, d.width, d.height, null);
			}
		};
		picturePanel.setOpaque(false);
		scrollMessage = new JScrollPane();
		taMessage = new JTextArea();
		scrollList = new JScrollPane();
		list = new JList<String>();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBackground(Color.black);
		setResizable(false);

		picturePanel.setBorder(null);

		GroupLayout picturePanelLayout = new GroupLayout(picturePanel);
		picturePanel.setLayout(picturePanelLayout);
		picturePanelLayout.setHorizontalGroup(picturePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 70, Short.MAX_VALUE));
		picturePanelLayout.setVerticalGroup(picturePanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));

		taMessage.setEditable(false);
		taMessage.setBackground(Color.black);
		taMessage.setColumns(20);
		taMessage.setForeground(Color.green);
		taMessage.setRows(4);
		taMessage.setWrapStyleWord(true);
		taMessage.setLineWrap(true);
		scrollMessage.setViewportView(taMessage);

		list.setBackground(Color.black);
		list.setForeground(Color.green);

		scrollList.setViewportView(list);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout
								.createParallelGroup(GroupLayout.Alignment.LEADING)
								.addComponent(scrollList)
								.addGroup(layout.createSequentialGroup().addComponent(picturePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(scrollMessage, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)))
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
								.addComponent(picturePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(scrollMessage))
						.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(scrollList, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE).addContainerGap()));

		pack();
		picturePanel.repaint();
		setLocationRelativeTo(null);
	}

	public void setErrors(final List<String> values) {
		list.setModel(new AbstractListModel<String>() {
			private static final long serialVersionUID = 0xA1L;
			List<String> elements = values;
			@Override
			public int getSize() {
				return elements.size();
			}

			@Override
			public String getElementAt(int index) {
				return elements.get(index);
			}

		});
	}
	
	public void setMessage(String message){
		this.taMessage.setText(message);
	}

	private JList<String> list;
	private JPanel picturePanel;
	private JScrollPane scrollList;
	private JScrollPane scrollMessage;
	private JTextArea taMessage;
}
